package pb

import (
	lua "github.com/yuin/gopher-lua"
)

// Preload adds strings to the given Lua state's package.preload table. After it
// has been preloaded, it can be loaded using require:
//
//  local pb = require("pb")
func Preload(L *lua.LState) {
	L.PreloadModule("pb", Loader)
}

// Loader is the module loader function.
func Loader(L *lua.LState) int {

	loggerUD := L.NewTypeMetatable(`pb_ud`)
	L.SetGlobal(`pb_ud`, loggerUD)
	L.SetField(loggerUD, "__index", L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{
		"configure": Configure,
		"start":     Start,
		"increment": Increment,
		"finish":    Finish,
	}))

	t := L.NewTable()
	L.SetFuncs(t, api)
	L.Push(t)
	return 1

}

var api = map[string]lua.LGFunction{
	"new": New,
}
